package com.emr.web.controller.emr;


import java.util.List;

import com.emr.common.utils.DateUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.emr.common.annotation.Log;
import com.emr.common.enums.BusinessType;
import com.emr.module.domain.Group;
import com.emr.module.service.IGroupService;
import com.emr.module.service.IPersonalService;
import com.emr.system.domain.SysUser;
import com.emr.system.service.ISysUserService;
import com.emr.web.core.base.BaseController;
import com.emr.framework.web.page.TableDataInfo;
import com.emr.common.base.AjaxResult;

/**
 * 院内会诊 信息操作处理
 * 
 * @author emr
 * @date 2019-02-28
 */
@Controller
@RequestMapping("/module/group")
public class GroupController extends BaseController
{
    private String prefix = "module/group";
	
	@Autowired
	private IGroupService groupService;
	@Autowired
	private IPersonalService ipersonalService ;
	@Autowired
	private ISysUserService isysUserService;
	
	
	@RequiresPermissions("module:group:view")
	@GetMapping()
	public String group()
	{
	    return prefix + "/group";
	}
	
	/**
	 * 查询院内会诊列表
	 */
	@RequiresPermissions("module:group:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(Group group)
	{
		SysUser user = getUser();
		group.setFaqiren(user.getUserName());
		startPage();
        List<Group> list = groupService.selectGroupList(group);
		return getDataTable(list);
	}
	
	/**
	 * 新增院内会诊
	 */
	@GetMapping("/add")
	public String add(ModelMap mmap)
	{
		SysUser user = getUser();
		mmap.put("dangqianyonghu", user.getUserName());
		mmap.put("yisheng", isysUserService.selectyishengAll());
		mmap.put("posts",ipersonalService.selectIcdAll());
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存院内会诊
	 */
	@RequiresPermissions("module:group:add")
	@Log(title = "院内会诊", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(Group group)
	{
		SysUser user = getUser();
		if(user!=null)group.setFaqiren(user.getUserName());
		return toAjax(groupService.insertGroup(group));
	}

	/**
	 * 修改院内会诊
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Integer id, ModelMap mmap)
	{
		Group group = groupService.selectGroupById(id);
		mmap.put("posts", ipersonalService.selectUPersonalByIds(Integer.valueOf(group.getHuizhenPersonald())));
//		mmap.put("hushi", isysUserService.selecthushiAlls(beds.getPersonalHushiid()));
		mmap.put("yisheng", isysUserService.selectyishengAlls(Integer.valueOf(group.getHuizhenZJ())));
		
		mmap.put("group", group);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存院内会诊
	 */
	@RequiresPermissions("module:group:edit")
	@Log(title = "院内会诊", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(Group group)
	{
		SysUser user = getUser();
		//判断：是修改会议添加当前用户为发起人
		if(user!=null&&"未知".equals(group.getYesOnNo()))group.setFaqiren(user.getUserName());
		//接收时，添加接收时间，判断会诊医生是否超时
		if("接受".equals(group.getYesOnNo()))group.setJieshousj(DateUtils.getDate());
		return toAjax(groupService.updateGroup(group));
	}
	
	/**
	 * 删除院内会诊
	 */
	@RequiresPermissions("module:group:remove")
	@Log(title = "院内会诊", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(groupService.deleteGroupByIds(ids));
	}
	
}
